using Godot;
using Godot.Collections;
using System;
using System.Collections.Generic;

public partial class SkillManager : Node
{
	const string BASEPATH = "res://resources/skills/";
	private Characters _character;
	public SkillBase CurrentSkill;
	// Called when the node enters the scene tree for the first time.
	public override void _Ready()
	{
		_character = GetParent<Characters>();
	}

	// Called every frame. 'delta' is the elapsed time since the previous frame.
	public override void _Process(double delta)
	{
		
	}

	public void UpdataSkills()
	{
		Player _player = _character as Player;
		foreach (Node child in this.GetChildren())
		{
			this.RemoveChild(child);
		}
		if(_player.itemLeftHand ==null)
		{return;}
		foreach (string skillname in _player.itemLeftHand.skills)
		{
			if(skillname == null)
			{return;}
			var path = BASEPATH + skillname + ".tscn";
			PackedScene prefab = GD.Load<PackedScene>(path);
			SkillBase skill = prefab.Instantiate<SkillBase>();
			GD.Print("添加技能：",skill.Name);
			this.AddChild(skill);
			

		}
	}

	public bool ChooseSkill()
	{
		if(_character._stateMachine.current_state == (int)Characters.State.SKILL)
		{return false;}
		foreach(var child in this.GetChildren())
		{
			SkillBase skill = child as SkillBase;
			if(skill.CanUse(_character.TargetDistance()))
			{
				CurrentSkill = skill;
				return true;//直接返回第一个能用的技能，待优化
			}
			if(skill == this.GetChild(-1))
			{
				//GD.Print("无可用技能");
				return false;
			}
		}
		return false;
	}
}
